﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Barbar.UrlGuard.Dal;

namespace Barbar.UrlGuard.BL.Concrete {
  internal class InjectorUtility : IInjectorUtility {
    #region IInjectorUtility Members

    public void SaveData<T>(T instance, Control rootControl) {
      if (rootControl == null)
        throw new ArgumentNullException("rootControl");
      Stack<Control> stack = new Stack<Control>();
      stack.Push(rootControl);
      while (stack.Count > 0) {
        Control control = stack.Pop();
        foreach (Control childControl in control.Controls) {
          stack.Push(childControl);
        }
        IInjector<T> injector = (control as IInjector<T>);
        if (injector != null)
          injector.SaveData(instance);
      }
    }

    public void LoadData<T>(T instance, Control rootControl) {
      if (rootControl == null)
        throw new ArgumentNullException("rootControl");

      Stack<Control> stack = new Stack<Control>();
      stack.Push(rootControl);
      while (stack.Count > 0) {
        Control control = stack.Pop();
        foreach (Control childControl in control.Controls) {
          stack.Push(childControl);
        }
        IReadOnlyInjector<T> injector = (control as IReadOnlyInjector<T>);
        if (injector != null)
          injector.LoadData(instance);
      }
    }

    #endregion
  }
}
